package com.rongxun.hbwallet.ui.fragment;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.rongxun.base.MvpFragment;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.HttpConstants;
import com.rongxun.hbwallet.constants.IntentKey;
import com.rongxun.hbwallet.constants.SPKey;
import com.rongxun.hbwallet.model.bean.CandyTask;
import com.rongxun.hbwallet.model.bean.Notice;
import com.rongxun.hbwallet.mvp.contract.IHome;
import com.rongxun.hbwallet.mvp.model.ApiModel;
import com.rongxun.hbwallet.mvp.presenter.HomePresenter;
import com.rongxun.hbwallet.ui.activities.LoginActivity;
import com.rongxun.hbwallet.ui.activities.MoreTaskActivity;
import com.rongxun.hbwallet.ui.activities.QRCodeScannerActivity;
import com.rongxun.hbwallet.ui.activities.ReceiptCodeActivity;
import com.rongxun.hbwallet.ui.activities.RechargeActivity;
import com.rongxun.hbwallet.ui.activities.TransferActivity;
import com.rongxun.hbwallet.ui.activities.WalletDetailActivity;
import com.rongxun.hbwallet.ui.activities.WalletManagerActivity;
import com.rongxun.hbwallet.ui.activities.WebActivity;
import com.rongxun.hbwallet.ui.adapter.HomeTaskAdapter;
import com.rongxun.utils.ActivityUtils;
import com.rongxun.utils.FlyLog;
import com.rongxun.utils.PreferenceUtils;
import com.rongxun.utils.StatusBarUtil;
import com.rongxun.utils.ToastUtils;
import com.rongxun.utils.wallet.ETHWallet;
import com.rongxun.utils.wallet.WalletDaoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/5/21 10:28
 * 描述  首页
 */

public class HomeFragment extends MvpFragment<IHome.IView, HomePresenter> implements IHome.IView {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    @BindView(R.id.home_viewFlipper)
    ViewFlipper mViewFlipper;

    @BindView(R.id.home_btn_wallet)
    LinearLayout mBtnWallet;
    @BindView(R.id.home_btn_scan)
    LinearLayout mHomeBtnScan;
    @BindView(R.id.home_btn_receiptcode)
    LinearLayout mHomeBtnReceiptcode;
    @BindView(R.id.home_btn_transfer)
    LinearLayout mHomeBtnTransfer;
    @BindView(R.id.home_tv_assets)
    TextView     mTvAssets;
    @BindView(R.id.home_btn_recharge)
    Button       mBtnRecharge;

    @BindView(R.id.home_iv_huobi)
    ImageView   mIvHuobi;
    @BindView(R.id.home_iv_bytetrade)
    ImageView   mIvBytetrade;
    @BindView(R.id.home_iv_bitcoin)
    ImageView   mIvBitcoin;
    @BindView(R.id.home_iv_ripple)
    ImageView   mIvRipple;
    @BindView(R.id.home_fl_moretask)
    FrameLayout moretask;
    @BindView(R.id.home_tv_notice)
    TextView    mTvNotice;

    @BindView(R.id.home_rv_task)
    RecyclerView mTask;
    private HomeTaskAdapter mHomeTaskAdapter;

    @Override
    public void showLoading(boolean isShow) {
    }

    @Override
    public void showError(String msg, String errorCode) {
    }

    @Override
    public void checkPermissionsSuccessful(String... permissions) {

    }

    @Override
    public void release() {
    }

    @Override
    protected void init() {
        StatusBarUtil.setToolBarPaddingTop(mToolbar, activity);

        mHomeTaskAdapter = new HomeTaskAdapter(R.layout.layout_hometask,mCandyTasks);

        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(activity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        mTask.setLayoutManager(linearLayoutManager);
        mTask.setAdapter(mHomeTaskAdapter);
    }

    @Override
    protected void onLazyLoadOnce() {
        super.onLazyLoadOnce();

        getPresenter().getCandyTask();
        getPresenter().getNotice("CN");
    }

    @NonNull
    @Override
    public HomePresenter createPresenter() {
        return new HomePresenter(this, new ApiModel(activity));
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_home;
    }

    @OnClick({R.id.home_btn_wallet, R.id.home_btn_scan, R.id.home_btn_receiptcode, R.id.home_btn_transfer, R.id.home_iv_huobi, R.id.home_iv_bytetrade,
            R.id.home_iv_bitcoin, R.id.home_iv_ripple, R.id.home_btn_recharge, R.id.home_fl_moretask, R.id.home_tv_notice})
    public void onViewClicked(View view) {
        String userId = PreferenceUtils.getString(SPKey.USERID, "0");
        if (Integer.parseInt(userId) == 0) {
            ActivityUtils.startActivityWithAnimotion(activity, new Intent(activity, LoginActivity.class));
            return;
        }
        ETHWallet ethWallet = WalletDaoUtils.checkRepeatByUserId(userId);
        if (ethWallet == null) {
            ActivityUtils.startActivityWithAnimotion(activity, new Intent(activity, WalletManagerActivity.class));
            return;
        }

        switch (view.getId()) {
            case R.id.home_btn_wallet:
                ActivityUtils.startActivityWithAnimotion(activity,
                        new Intent(activity, WalletDetailActivity.class)
                                .putExtra(IntentKey.ETH_WALLET, ethWallet));
                break;
            case R.id.home_btn_scan:

                ActivityUtils.startActivityWithAnimotion(activity,
                        new Intent(activity, QRCodeScannerActivity.class).putExtra(IntentKey.SCAN_FROM, QRCodeScannerActivity.HOME));
                break;
            case R.id.home_btn_receiptcode:
                ActivityUtils.startActivityWithAnimotion(activity,
                        new Intent(activity, ReceiptCodeActivity.class)
                                .putExtra(IntentKey.ETH_WALLET, ethWallet));
                break;
            case R.id.home_btn_transfer:
                ActivityUtils.startActivityWithAnimotion(activity,
                        new Intent(activity, TransferActivity.class)
                                .putExtra(IntentKey.ETH_WALLET, ethWallet));
                break;
            case R.id.home_fl_moretask:
                if (mCandyTasks != null&&mCandyTasks.size()>0) {
                    ActivityUtils.startActivityWithAnimotion(activity,
                            new Intent(activity, MoreTaskActivity.class).putParcelableArrayListExtra(IntentKey.MORETASKS,mCandyTasks));
                }else{
                    ToastUtils.showToast(R.string.no_tasks);
                }

                break;
            case R.id.home_iv_huobi:
                ToastUtils.showToast("go to huobi");
                break;
            case R.id.home_iv_bytetrade:
                ToastUtils.showToast("go to bytetrade");
                break;
            case R.id.home_iv_bitcoin:
                ToastUtils.showToast("go to bitcoin");
                break;
            case R.id.home_iv_ripple:
                ToastUtils.showToast("go to ripple");
                break;
            case R.id.home_btn_recharge:
                ActivityUtils.startActivityWithAnimotion(activity,
                        new Intent(activity, RechargeActivity.class)
                                .putExtra(IntentKey.ETH_WALLET, ethWallet));
                break;
            case R.id.home_tv_notice:
                ActivityUtils.startActivityWithAnimotion(activity,
                        new Intent(activity, WebActivity.class)
                                .putExtra(IntentKey.WEB_URL, HttpConstants.WEB_ADDRESS.NOTICE_URL));
                break;
        }
    }

    @Override
    public void showNotice(List<Notice.NoticeBean> notice) {
        for (Notice.NoticeBean noticeBean : notice) {
            String text = noticeBean.getText();
            TextView textView = new TextView(activity);
            textView.setText(text);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.x39));
            textView.setSingleLine(true);
            textView.setOnClickListener(v -> {
                String noticeBeanId = noticeBean.getId();
                ToastUtils.showToast(noticeBeanId);
                ActivityUtils.startActivityWithAnimotion(activity,
                        new Intent(activity, WebActivity.class)
                                .putExtra(IntentKey.WEB_URL, HttpConstants.WEB_ADDRESS.NOTICEDETAILS_URL + "?id=" + noticeBean.getId()));

            });
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mViewFlipper.addView(textView, lp);
        }
        mViewFlipper.startFlipping();
    }

    ArrayList<CandyTask> mCandyTasks = new ArrayList<>();

    @Override
    public void showCandyTask(List<CandyTask> candyTasks) {
        FlyLog.i("candyTasks"+candyTasks);
        mCandyTasks.clear();
        mCandyTasks.addAll(candyTasks);
        mHomeTaskAdapter.notifyDataSetChanged();
    }
}